package sdk.login.tvos.com.danmutest02;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextPaint;

import java.util.Map;

import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.android.SpannedCacheStuffer;

public class MyCacheStuffer extends SpannedCacheStuffer {
    private int HeadImage_Height; //头像直径
    private int HeadImage_Border; // 头像边框宽度
    private int TextPadiing_Left; // 文字和头像间距
    private int TextPadding_Right; // 文字和右边线距离
    private int Text_Size; // 内容文字大小
    private int Text_bg_Height;
    private int Text_Color = 0xff2ad4db;  //内容文字颜色
    private int Text_bg_Color = 0xccffffff; // 文字灰色背景色值
    private int Text_bg__Radius; // 文字灰色背景圆角
    private int Text_Name_Size;
    private int Text_Name_Color=0xff848484;

    public MyCacheStuffer(Context context) {
        // 初始化固定参数，这些参数可以根据自己需求自行设定
        HeadImage_Height = 88;
        HeadImage_Border = 2;
        TextPadiing_Left = 10;
        TextPadding_Right = 30;
        Text_bg_Height=80;
        Text_Size = 28;
        Text_bg__Radius = 50;
        Text_Name_Size=28;




    }

    @Override
    public void measure(BaseDanmaku danmaku, TextPaint paint, boolean fromWorkerThread) {
        // 初始化数据
        Map<String, Object> map = (Map<String, Object>) danmaku.tag;
        String content = (String) map.get("content");
        String name= (String) map.get("name");

        // 设置画笔
        paint.setTextSize(Text_Size);
        // 计算内容的长度
        float contentWidth = paint.measureText(content);
        float nameWitdth=paint.measureText(name);
        float TextWidth=Math.max(contentWidth,nameWitdth);
        // 设置弹幕区域的宽高
        danmaku.paintWidth = TextWidth + HeadImage_Height + HeadImage_Border * 2 + TextPadiing_Left + TextPadding_Right; // 设置弹幕区域的宽度
        danmaku.paintHeight = HeadImage_Height + HeadImage_Border * 2; // 设置弹幕区域的高度
    }

    @Override
    public void drawStroke(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, Paint paint) {
    }

    @Override
    public void drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, TextPaint paint, boolean fromWorkerThread) {

    }

    @Override
    public void clearCaches() {
    }

    @Override
    public void drawBackground(BaseDanmaku danmaku, Canvas canvas, float left, float top) {
        // 初始化数据
        Map<String, Object> map = (Map<String, Object>) danmaku.tag;
        String content = (String) map.get("content");
        Bitmap bitmap = (Bitmap) map.get("bitmap");
        String name= (String) map.get("name");

        Paint paint = new Paint();
        // 设置画笔
        paint.setTextSize(Text_Size);

        // 绘制文字背景
        Rect rect = new Rect();
        paint.getTextBounds(content, 0, content.length(), rect);
        paint.setColor(Text_bg_Color);
        paint.setAntiAlias(true);
        float bgLeft = left + HeadImage_Height / 2 + HeadImage_Border/2-10;//-的pix 动态调整
        float bgRight = left + HeadImage_Height + HeadImage_Border * 2 + TextPadiing_Left + rect.width() + TextPadding_Right;
        float mybgTop=top+danmaku.paintHeight/2-(Text_bg_Height/2);
        float mybgBottom=mybgTop+Text_bg_Height;
        canvas.drawRoundRect(new RectF(bgLeft, mybgTop, bgRight, mybgBottom), Text_bg__Radius, Text_bg__Radius, paint);

        // 绘制头像背景
        paint.setColor(Color.WHITE);
        float centerX = left + HeadImage_Height / 2 + HeadImage_Border;
        float centerY = left + HeadImage_Height / 2 + HeadImage_Border;
        float radius = HeadImage_Height / 2 + HeadImage_Border; // 半径
        canvas.drawCircle(centerX, centerY, radius, paint);

        // 绘制头像
        float avatorLeft = left + HeadImage_Border;
        float avatorTop = top + HeadImage_Border;
        float avatorRight = left + HeadImage_Border + HeadImage_Height;
        float avatorBottom = top + HeadImage_Border + HeadImage_Height;
        canvas.drawBitmap(bitmap, null, new RectF(avatorLeft, avatorTop, avatorRight, avatorBottom), paint);

        // 绘制弹幕内容
        paint.setColor(Text_Color);
        float contentLeft = left + HeadImage_Height + HeadImage_Border * 2 + TextPadiing_Left;
        //float myContentBottom=danmaku.paintHeight/2+ Text_Size /2-DTLUtils.getPixels(5);
        canvas.drawText(content, contentLeft, mybgBottom-10, paint); //-的值 动态调整


        //绘制名字
        paint.setTextSize(Text_Name_Size);
        paint.setColor(Text_Name_Color);
        float nameBottom=mybgBottom-10-Text_Size-10;
        canvas.drawText(name, contentLeft, nameBottom, paint);

    }
}
